MessbereichscheckSgl <- function(inpath, outpath, para = c("Ta_200", "rH_200", "Ta_10",
                                                     "Ts_5", "Ts_10", "Ts_20",
                                                     "Ts_50", "SM_10", "SM_15", 
                                                     "SM_20", "SM_30", "SM_40", 
                                                     "SM_50", "PAR_300", "Albedo",
                                                     "SWDR_300", "SWUR_300", "WV", 
                                                     "P_container_RT", 
                                                     "P_container_NRT"), digit){
  
  ##input
   df <- read.table(inpath, header = F, sep = ",", fill = T, stringsAsFactors = F)
  
    qual <- rep("0000000000000000", length(df[,1]))
    qual <- as.character(df[["Qualityflag"]])

    qualleft <- substr(qual, 1, (digit - 1))
    qualact <- substr(qual, digit, (digit + 1))
    qualright <- substr(qual, (digit + 2), length(qual))
    df[[para]] <- as.numeric(df[[para]])  
  ## test section
    qualact <- ifelse(df[[para]] > maxlimit, "r2", "r1")

      
    qualact <- switch(para,
                   "Ta_200" = ifelse(df[[para]] < -40 || df[[para]] > 60, "r1", "r2"),
                   "rH_200" = ifelse(df[[para]] < 0 || df[[para]] > 100, "r1", "r2"),
                   "Ta_10" = ifelse(df[[para]] < -40 || df[[para]] > 60, "r1", "r2"),
                   "Ts_5" = ifelse(df[[para]] < -40 || df[[para]] > 60, "r1", "r2"),
                   "Ts_10" = ifelse(df[[para]] < -40 || df[[para]] > 60, "r1", "r2"),
                   "Ts_20" = ifelse(df[[para]] < -40 || df[[para]] > 60, "r1", "r2"),
                   "Ts_50" = ifelse(df[[para]] < -40 || df[[para]] > 60, "r1", "r2"),
                   "SM_10" = ifelse(df[[para]] < 0, "r1", "r2"),
                   "SM_15" = ifelse(df[[para]] < 0, "r1", "r2"),
                   "SM_20" = ifelse(df[[para]] < 0, "r1", "r2"),
                   "SM_30" = ifelse(df[[para]] < 0, "r1", "r2"),
                   "SM_40" = ifelse(df[[para]] < 0, "r1", "r2"),
                   "SM_50" = ifelse(df[[para]] < 0, "r1", "r2"),
                   "PAR_300" = ifelse(df[[para]] < 0, "r1", "r2"),
                   "Albedo" = ifelse(df[[para]] < 0 || df[[para]] > 1, "r1", "r2"),
                   "SWDR_300" = ifelse(df[[para]] < 0, "r1", "r2"),
                   "SWUR_300" = ifelse(df[[para]] < 0, "r1", "r2"),
                   "WV" = ifelse(df[[para]] < 0, "r1", "r2"),    
                   "P_container_RT" = ifelse(df[[para]] < 0, "r1", "r2"),
                   "P_container_NRT" = ifelse(df[[para]] < 0, "r1", "r2"))
      
  qual <- paste(qualleft, qualact, qualright, sep = "")
   
  df["Qualityflag"] <- qual
   print(qual)
  write.table(df, outpath, col.names = T, row.names = F, sep = ",")
}

## EXAMPLE
MessbereichscheckSgl(
inpath = "C:/Users/Steffi/Documents/=Uni/SpezPhy/Daten/be_000HEG01_00CEMU_200912010000_200912302330_mez_ca05_cti30_0005.dat",
outpath = "C:/Users/Steffi/Documents/=Uni/SpezPhy/Daten/Out/be_000HEG01_00CEMU_200911010000_200911302330_mez_ca05_cti30_0010.dat",
para = "SM_10",
digit = 3)
